package com.tmsps.common.bean;

import com.tmsps.ne4spring.orm.param.NeParamList;
import com.tmsps.ne4spring.utils.ChkUtil;
import com.tmsps.ne4spring.utils.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * sql辅助Bean
 *
 * @author geek
 * @date 2020-05-09
 * @version V1.0
 */
public class ParamBean {

	private Logger log = LoggerFactory.getLogger(ParamBean.class);
	
	private NeParamList params = new NeParamList();
	private StringBuffer sql = new StringBuffer();
	private StringBuffer from = new StringBuffer();
	
	
	/**
	 * @title sqlAppend
	 * @description sql语句拼接
	 * @param sql
	 * return void
	 */
	public void sqlAppend(String sql) {
		this.sql.append(this.trim(sql));
	}
	/**
	 * @title sqlAppend
	 * @description sql语句拼接
	 * @param sql
	 * @param param
	 * return void
	 */
	public void sqlAppend(String sql,String param) {
		if(StrUtil.notBlank(param)){
			this.sql.append(this.trim(sql));
			this.params.add(param);
		}
	}
	/**
	 * @title sqlAppend
	 * @description sql语句拼接
	 * @param sql
	 * @param params
	 * return void
	 */
	public void sqlAppend(String sql,String... params) {
		boolean flag = true;
		if(params==null || params.length==0) {
			return ;
		}
		for(String param:params){
			if(StrUtil.isBlank(param)){
				flag = false;
				break;
			}
			this.params.add(param);
		}
		if(flag) {
			this.sql.append(this.trim(sql));
		}
	}
	/**
	 * @title sqlAppend
	 * @description sql语句拼接
	 * @param sql
	 * @param param Integer
	 * return void
	 */
	public void sqlAppend(String sql,Integer param) {
		if(param != null && isNumeric(param.toString())){
			this.sql.append(this.trim(sql));
			this.params.add(param);
		}
	}
	/**
	 * @title sqlAppend
	 * @description sql语句拼接
	 * @param sql
	 * @param params Integer
	 * return void
	 */
	public void sqlAppend(String sql,Integer... params) {
		boolean flag = true;
		if(params==null || params.length==0) {
			return ;
		}
		for(Integer param:params){
			if(param == null){
				flag = false;
				break;
			}
			this.params.add(param);
		}
		if(flag) {
			this.sql.append(this.trim(sql));
		}
	}
	/**
	 * @title sqlAppendLike
	 * @description sql语句拼接
	 * @param sql
	 * @param param
	 * return void
	 */
	public void sqlAppendLike(String sql,String param) {
		if(StrUtil.notBlank(param)){
			this.sql.append(this.trim(sql));
			this.params.addLike(param);
		}
	}
	/**
	 * @title sqlAppendLike
	 * @description sql语句拼接
	 * @param sql
	 * @param params
	 * return void
	 */
	public void sqlAppendLike(String sql,String... params) {
		boolean flag = true;
		if(params==null || params.length==0) {
			return ;
		}
		for(String param:params){
			if(StrUtil.isBlank(param)){
				flag = false;
				break;
			}
			this.params.addLike(param);
		}
		if(flag) {
			this.sql.append(this.trim(sql));
		}
	}
	
	/**
	 * @title sqlAppend
	 * @description sql语句拼接
	 * @param sql
	 * @param param Object
	 * return void
	 */
	public void sqlAppend(String sql,Object param) {
		if(ChkUtil.isNotNull(param)){
			this.sql.append(this.trim(sql));
			this.params.add(param);
		}
	}
	
	/**
	 * @title fromAppend
	 * @description from语句拼接
	 * @param sql
	 * return void
	 */
	public void fromAppend(String sql) {
		this.from.append(this.trim(sql));
	}
	/**
	 * @title fromAppend
	 * @description from语句拼接
	 * @param sql
	 * @param param
	 * return void
	 */
	public void fromAppend(String sql,String param) {
		if(StrUtil.notBlank(param)){
			this.from.append(this.trim(sql));
			this.params.add(param);
		}
	}
	/**
	 * @title fromAppend
	 * @description from语句拼接
	 * @param sql
	 * @param params
	 * return void
	 */
	public void fromAppend(String sql,String... params) {
		boolean flag = true;
		if(params==null || params.length==0) {
			return ;
		}
		for(String param:params){
			if(StrUtil.isBlank(param)){
				flag = false;
				break;
			}
			this.params.add(param);
		}
		if(flag) {
			this.from.append(this.trim(sql));
		}
	}
	/**
	 * @title fromAppendLike
	 * @description from语句拼接
	 * @param sql
	 * @param param
	 * return void
	 */
	public void fromAppendLike(String sql,String param) {
		if(StrUtil.notBlank(param)){
			this.from.append(this.trim(sql));
			this.params.addLike(param);
		}
	}
	/**
	 * @title fromAppendLike
	 * @description from语句拼接
	 * @param sql
	 * @param params
	 * return void
	 */
	public void fromAppendLike(String sql,String... params) {
		boolean flag = true;
		if(params==null || params.length==0) {
			return ;
		}
		for(String param:params){
			if(StrUtil.isBlank(param)){
				flag = false;
				break;
			}
			this.params.addLike(param);
		}
		if(flag) {
			this.from.append(this.trim(sql));
		}
	}
	/**
	 * @title fromAppend
	 * @description from语句拼接
	 * @param sql
	 * @param param Integer
	 * return void
	 */
	public void fromAppend(String sql,Integer param) {
		if(param != null && isNumeric(param.toString())){
			this.from.append(this.trim(sql));
			this.params.add(param);
		}
	}
	/**
	 * @title fromAppend
	 * @description from语句拼接
	 * @param sql
	 * @param params Integer
	 * return void
	 */
	public void fromAppend(String sql,Integer... params) {
		boolean flag = true;
		if(params==null || params.length==0) {
			return ;
		}
		for(Integer param:params){
			if(param == null){
				flag = false;
				break;
			}
			this.params.add(param);
		}
		if(flag) {
			this.from.append(this.trim(sql));
		}
	}
	/**
	 * @title fromAppend
	 * @description from语句拼接
	 * @param sql
	 * @param param Object
	 * return void
	 */
	public void fromAppend(String sql,Object param) {
		if(ChkUtil.isNotNull(param)){
			this.from.append(this.trim(sql));
			this.params.add(param);
		}
	}
	
	public NeParamList getParams() {
		return params;
	}
	
	public Object[] getParamValues() {
		return params.getParamValues();
	}
	
	public String getSql() {
		String sqlStr = sql.toString();
		log.debug("sql语句：{}",sqlStr);
		return sqlStr;
	}
	
	public String getFrom() {
		String fromStr = from.toString();
		log.debug("from语句：{}",fromStr);
		return fromStr;
	}
	
	/**
	 * @title isNumeric
	 * @description 判断是否是整数
	 * @author hanjiefei
	 * @date 2020-08-16
	 * @param source
	 * return boolean
	 */
	private boolean isNumeric(String source) {
		Pattern pattern = Pattern.compile("[0123456789-]*");
		Matcher isNum = pattern.matcher(source);
		return isNum.matches();
	}
	/**
	 * @Title trim
	 * @Description 处理sql语句空格缺失
	 * @author hanjiefei
	 * @date 2020-11-05
	 * @param source
	 * @return String
	 */
	private String trim(String source) {
		if (StrUtil.notBlank(source)) {
			return source.trim()+" ";
		}
		return source;
	}

}
